package com.esgpro.esg_api.controller;

import com.alibaba.fastjson.JSON;
import com.esgpro.esg_api.pojo.Browse;
import com.esgpro.esg_api.service.Impl.BrowseServiceImpl;
import com.esgpro.esg_api.util.DateUtil;
import com.esgpro.esg_api.util.JSONResult;
import com.esgpro.esg_api.util.StringUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "浏览记录接口列表")
@RequestMapping("/browse")
public class BrowseController {
    @Autowired
    BrowseServiceImpl service;
    @Autowired
    JSONResult result;
    @Autowired
    DateUtil util;

    @PostMapping("/add")
    public JSON insertMessage(@RequestParam int user_id, @RequestParam int comp_id) {
        return result.getResult(service.insertOne(new Browse(0, user_id, comp_id, util.getDate(), false)));
    }

    @DeleteMapping("/delete")
    public JSON deleteMessageById(@RequestParam int id) {
        return result.getResult(service.deleteOneById(id));
    }

    @GetMapping("/selectOne")
    public JSON selectMessageById(@RequestParam int id) {
        return result.getDataResult(service.selectOneById(id));
    }

    @PostMapping("/selectMap")
    public JSON selectMessageByMap(@RequestParam int current, @RequestParam int size, @RequestParam(required = false) String keys, @RequestParam(required = false) String values) {
        HashMap<String, Object> map = new HashMap<>();
        List<String> key = StringUtil.StringFormat(keys);
        List<String> value = StringUtil.StringFormat(values);
        System.out.println("keys:" + key);
        System.out.println("values:" + value);
        for (int i = 0; i < key.size(); i++)
            map.put(key.get(i), value.get(i));
        return result.getDataResult(service.selectByMap(current, size, map));
    }

    @PutMapping("/update")
    public JSON updateMessageById(@RequestParam Integer id, @RequestParam int user_id, @RequestParam int comp_id, @RequestParam String date, @RequestParam boolean isDelete) {
        return result.getResult(service.updateById(new Browse(id, user_id, comp_id, date, isDelete)));
    }

    @GetMapping("/selectAll")
    public JSON selectAll() {
        return result.getDataResult(service.selectAll());
    }
}
